package org.whb.basic.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.whb.basic.dto.LoginDto;
import org.whb.basic.service.ILoginService;
import org.whb.basic.util.AjaxResult;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    private AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return new AjaxResult().setResultObj(map);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("服务器出错啦，请稍后再试！！！");
        }
    }

    /**
     * 微信登录
     * @param param
     * @return
     */
    @PostMapping("/wechat")
    private AjaxResult wechat(@RequestBody Map<String,String> param){
        try {
            AjaxResult ajaxResult = loginService.wechat(param);
            return ajaxResult;
        }catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("服务器出错啦，请稍后再试！！！");
        }
    }


    /**
     * 微信绑定
     * @param param
     * @return
     */
    @PostMapping("/binder/wechat")
    private AjaxResult binderWechat(@RequestBody Map<String,String> param){
        try {
            Map<String, Object> map=loginService.binderWechat(param);
            return new AjaxResult().setResultObj(map);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("服务器出错啦，请稍后再试！！！");
        }
    }

}
